//
//  ZDSafeMutableArray.m
//  Location_Demo
//
//  Created by JXH on 2021/1/14.
//

#import "ZDSafeMutableArray.h"

@interface ZDSafeMutableArray()
/** 数据array*/
@property (nonatomic, strong) NSMutableArray *safaArray;
/** 操作并发队列*/
@property (nonatomic, strong) dispatch_queue_t syncQueue;

@end

@implementation ZDSafeMutableArray
#pragma mark - -------------- lazy load -----------------
- (dispatch_queue_t)syncQueue {
    if (_syncQueue == nil) {
        _syncQueue = dispatch_queue_create("ioSafeQueue", DISPATCH_QUEUE_CONCURRENT);
    }
    return _syncQueue;
}
#pragma mark - -------------- init -----------------
- (instancetype)init {
    if (self = [super init]) {
        _safaArray = [NSMutableArray new];
    }
    return self;
}
+ (instancetype)safetyArray {
    return [[self alloc] init];
}
+ (instancetype)safetyArrayWithArray:(NSArray *)array {
    return [[self alloc] initWithArray:array];
}
+ (instancetype)arrayWithCapacity:(NSUInteger)capacity {
    return [[self alloc] initWithCapacity:capacity];
}
- (instancetype)initWithArray:(NSArray *)array {
    if (self = [super init]) {
        _safaArray = array? [NSMutableArray arrayWithArray:array]:[NSMutableArray array];
    }
    return self;
}
- (instancetype)initWithCapacity:(NSUInteger)capacity {
    if (self = [super init]) {
        _safaArray = capacity? [NSMutableArray arrayWithCapacity:capacity]:[NSMutableArray array];
    }
    return self;
}
#pragma mark - -------------- read -----------------
//集合元素数量
- (NSUInteger)count {
    __block NSUInteger count;
    dispatch_sync(self.syncQueue, ^{
        count = _safaArray.count;
    });
    return count;
}
//获取元素
- (id)objectAtIndex:(NSUInteger)index {
    
    __block id obj;
    dispatch_sync(self.syncQueue, ^{
        if (index <_safaArray.count) {
            obj = _safaArray[index];
        }
    });
    return obj;
}
//判断是否包含对象
- (BOOL)containsObject:(id)anObject {
    __block BOOL flag = NO;
    dispatch_sync(self.syncQueue, ^{
        flag = [_safaArray containsObject:anObject];
    });
    return flag;
}
//获取索引
- (NSUInteger)indexOfObject:(id)anObject {
    __block NSUInteger index;
    dispatch_sync(self.syncQueue, ^{
        index = [_safaArray indexOfObject:anObject];
    });
    return index;
}
#pragma mark - -------------- write -----------------
//插入
- (void)addObject:(id)anObject {
    __weak typeof(self) weakSelf = self;
    dispatch_barrier_async(self.syncQueue, ^{
        if (anObject) {
            [weakSelf.safaArray addObject:anObject];
        }
    });
}
//插入
- (void)insertObject:(id)anObject atIndex:(NSUInteger)index {
    __weak typeof(self) weakSelf = self;
    dispatch_barrier_async(self.syncQueue, ^{
        if (anObject) {
            [weakSelf.safaArray insertObject:anObject atIndex:index];
        }
    });
}
//替换
- (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject {
    __weak typeof(self) weakSelf = self;
    dispatch_barrier_async(self.syncQueue, ^{
        if (anObject) {
            [weakSelf.safaArray replaceObjectAtIndex:index withObject:anObject];
        }
    });
}
//移除
- (void)removeObjectAtIndex:(NSUInteger)index {
    __weak typeof(self) weakSelf = self;
    dispatch_barrier_async(self.syncQueue, ^{
        if (index < weakSelf.safaArray.count && index >= 0) {
            [weakSelf.safaArray removeObjectAtIndex:index];
        }
    });
}
//移除
- (void)removeObject:(id)anObject {
    __weak typeof(self) weakSelf = self;
    dispatch_barrier_async(self.syncQueue, ^{
        if (anObject) {
            [weakSelf.safaArray removeObject:anObject];
        }
    });
}
//移除
- (void)removeLastObject {
    __weak typeof(self) weakSelf = self;
    dispatch_barrier_async(self.syncQueue, ^{
        if (weakSelf.safaArray.count) {
            [weakSelf.safaArray removeLastObject];
        }
    });
}
//移除
- (void)removeAllObjects {
    __weak typeof(self) weakSelf = self;
    dispatch_barrier_async(self.syncQueue, ^{
        if (weakSelf.safaArray.count) {
            [weakSelf.safaArray removeAllObjects];
        }
    });
}
#pragma mark - -------------- desc -----------------
- (NSString *)description {
    return [NSString stringWithFormat:@"%@",self.safaArray];
}
- (void)dealloc
{
    if (_syncQueue) {
        _syncQueue = NULL;
    }
}
@end
